package top.glkj.hms.utils;

import org.springframework.data.domain.Page;

import java.util.List;

/**
 * @author : xgl
 * @version : 0.0.1
 * @date :2020/11/10 18:48
 */
public final class ResultFactory {
    public final static Result SUCCESS = isSuccess();
    public final static Result ERROR = isError();
    public final static Result NOTFOUND = isNotFound();
    public final static Result EXCEPTION = isException();
    public final static Result REDIRECTION = isRedirection();
    public final static Result DEFINE = isRedirection();

    public static Result isSuccess() {
        return isSuccess("");
    }

    public static Result isSuccess(String msg) {
        return isSuccess(msg, null);
    }

    public static Result isSuccess(Object obj) {
        return isSuccess("", obj);
    }

    public static Result isSuccess(String msg, Object o) {
        return new Result(200, msg, o);
    }

    public static Result isError() {
        return isError("");
    }

    public static Result isError(String msg) {
        return isError(msg, null);
    }

    public static Result isError(Object obj) {
        return isError("", obj);
    }

    public static Result isError(String msg, Object o) {
        return new Result(500, msg, o);
    }

    public static Result isRedirection() {
        return isRedirection("");
    }

    public static Result isRedirection(String msg) {
        return isRedirection(msg, null);
    }

    public static Result isRedirection(Object obj) {
        return isRedirection("", obj);
    }

    public static Result isRedirection(String msg, Object o) {
        return new Result(301, msg, o);
    }

    public static Result isNotFound() {
        return isNotFound("");
    }

    public static Result isNotFound(Object obj) {
        return isNotFound("", obj);
    }

    public static Result isNotFound(String msg) {
        return isNotFound(msg, null);
    }

    public static Result isNotFound(String msg, Object o) {
        return new Result(404, msg, o);
    }

    public static Result isException() {
        return isException("");
    }

    public static Result isException(String msg) {
        return isException(msg, null);
    }

    public static Result isException(Object obj) {
        return isException("", obj);
    }

    public static Result isException(String msg, Object o) {
        return new Result(502, msg, o);
    }

    public static Result isDefine() {
        return isDefine("");
    }

    public static Result isDefine(String msg) {
        return isDefine(msg, null);
    }

    public static Result isDefine(Object obj) {
        return isDefine("", obj);
    }

    public static Result isDefine(String msg, Object o) {
        return isDefine(0, msg, o);
    }

    public static Result isDefine(int code, Object o) {
        return isDefine(code, "", o);
    }

    public static Result isDefine(int code, String msg) {
        return isDefine(code, msg, null);
    }

    public static Result isDefine(int code, String msg, Object o) {
        return new Result(code, msg, o);
    }

    public static boolean isSuccess(Result result) {
        return result.getCode() == SUCCESS.getCode();
    }

    public static boolean isError(Result result) {
        return result.getCode() == ERROR.getCode();
    }

    public static boolean isNotFound(Result result) {
        return result.getCode() == NOTFOUND.getCode();
    }

    ;

    public static boolean isException(Result result) {
        return result.getCode() == EXCEPTION.getCode();
    }

    ;

    public static boolean isRedirection(Result result) {
        return result.getCode() == REDIRECTION.getCode();
    }

    ;
    public static Result auto(Object o) {
        if (o instanceof String) {
            if (o.toString().contains(StaticValue.ERROR)) {
                return ResultFactory.isError(o);
            } else if (o.toString().contains(StaticValue.NOT_FOUND)){
                return ResultFactory.isNotFound(o);
            }else if (o.toString().contains(StaticValue.EXCEPTION)||o.toString().contains(StaticValue.FAIL)){
                return ResultFactory.isException(o);
            }else{
                return ResultFactory.isSuccess(o);
            }
        } else if (o instanceof List) {
            if (((List<?>) o).isEmpty()) {
                return ResultFactory.isNotFound(StaticValue.DATA_NOT_FOUND);
            } else {
                return ResultFactory.isSuccess(StaticValue.GET_DATA_SUCCESS, o);
            }
        } else if (o instanceof Page) {
            if (((Page<?>) o).isEmpty()) {
                return ResultFactory.isNotFound(StaticValue.DATA_NOT_FOUND);
            } else {
                return ResultFactory.isSuccess(StaticValue.GET_DATA_SUCCESS, o);
            }
        }else {
            if (o == null){
                return ResultFactory.isNotFound(StaticValue.DATA_NOT_FOUND);
            } else {
                return ResultFactory.isSuccess(StaticValue.GET_DATA_SUCCESS, o);
            }
        }
    }

    ;
}
